﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ProjectSunny.Admin.Controls
{
    public partial class AddCategory : System.Web.UI.UserControl
    {
        public int intactivetop, intactiveleft;
        public string filemap;
        public string pathToCreate;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                // Them du lieu vao cat 1
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2","0" );
                ListItem lt = new ListItem("Chon danh muc", "-1");
                Cate1.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate1.Items.Add(lt);
                }
                Cate1.SelectedIndex = 0;
            }
        }

        protected void Cate1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Chon danh muc cap 1 load danh muc cap 2
            Cate2.Items.Clear();
            Cate3.Items.Clear();
            int index = Cate1.SelectedIndex;
            if (index > 0)
            {
                int id = Convert.ToInt32(Cate1.SelectedValue);
                // Load du lieu
                DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category", "Category_ID2", id.ToString());
                ListItem lt = new ListItem("Chon danh muc", "-1");
                Cate2.Items.Add(lt);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                    Cate2.Items.Add(lt);
                }               
                    txtNameCategory.Enabled = false;
                    fulImageCategory.Enabled = false;
                    CKEditorMota.Enabled = false;
                    ddlActiveLeft.Enabled = false;
                    ddlActiveTop.Enabled = false;
                    btnAddCategory.Enabled = false;
                    btnReset.Enabled = false;
                    Cate2.Enabled = true;
                    btnCate2.Enabled = true;
            }
        }

        protected void Cate2_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Chon danh muc cap 1 load danh muc cap 2
            Cate3.Items.Clear();
            int index = Cate2.SelectedIndex;

            int id = Convert.ToInt32(Cate2.SelectedValue);
            // Load du lieu
            DataTable dt = DAL.DCollector.DBCollector.Instance.StoredSelectTableWhereID("tbl_Category","Category_ID2", id.ToString());
            ListItem lt = new ListItem("Chon danh muc", "-1");
            Cate3.Items.Add(lt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                lt = new ListItem(dt.Rows[i]["Category_Name"].ToString(), dt.Rows[i]["Category_ID1"].ToString());
                Cate3.Items.Add(lt);
            }
            txtNameCategory.Enabled = false;
            fulImageCategory.Enabled = false;
            CKEditorMota.Enabled = false;
            ddlActiveLeft.Enabled = false;
            ddlActiveTop.Enabled = false;
            btnAddCategory.Enabled = false;
            btnReset.Enabled = false;
            Cate3.Enabled = true;
            btnCate3.Enabled = true;
        }

        protected void btnAddCate1_Click(object sender, EventArgs e)
        {
            txtNameCategory.Enabled = true;
            fulImageCategory.Enabled = true;
            CKEditorMota.Enabled = true;
            ddlActiveLeft.Enabled = true;
            ddlActiveTop.Enabled = true;
            btnAddCategory.Enabled = true;
            btnReset.Enabled = true;
            lblIndex.Text = "1";
            lbldanhmuc.Text = "Bạn đang thêm danh mục cấp 1";

        }

        protected void btnCate2_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Cate1.SelectedValue);
            DataTable dt = DAL.DCollector.DBCollector.Instance.CallSQL1Param("callstored", "SELECT count(*) as result FROM tbl_ThietKeWebsite WHERE Category_ID3 = " + id + "", true);
            if (dt.Rows[0]["result"].ToString() == "0")
            {
                txtNameCategory.Enabled = true;
                fulImageCategory.Enabled = true;
                CKEditorMota.Enabled = true;
                ddlActiveLeft.Enabled = true;
                ddlActiveTop.Enabled = true;
                btnAddCategory.Enabled = true;
                btnReset.Enabled = true;
                btnCate2.Enabled = true;
                lblIndex.Text = "2";
                lbldanhmuc.Text = "Bạn đang thêm danh mục cấp 2";
            }
            else
            {
                txtNameCategory.Enabled = false;
                fulImageCategory.Enabled = false;
                CKEditorMota.Enabled = false;
                ddlActiveLeft.Enabled = false;
                ddlActiveTop.Enabled = false;
                btnAddCategory.Enabled = false;
                btnReset.Enabled = false;
                Cate2.Enabled = false;
                btnCate2.Enabled = true;
                Response.Write("<script>alert('Danh mục 1 đã có sản phẩm . Bạn không thể thêm danh mục 2')</script>");
            }

            
        }

        protected void btnCate3_Click(object sender, EventArgs e)
        {
            txtNameCategory.Enabled = true;
            fulImageCategory.Enabled = true;
            CKEditorMota.Enabled = true;
            ddlActiveLeft.Enabled = true;
            ddlActiveTop.Enabled = true;
            btnAddCategory.Enabled = true;
            btnReset.Enabled = true;
            lblIndex.Text = "3";
            lbldanhmuc.Text = "Bạn đang thêm danh mục cấp 3";
        }

        protected void btnAddCategory_Click(object sender, EventArgs e)
        {
            string filePathIamgeCate = null;
            string cate1;
            string cate2;
            string activetop = ddlActiveTop.Text;
            string activeleft = ddlActiveLeft.Text;
            if (lblIndex.Text.Equals("1"))
            {
                if (activetop == "Có")
                {
                    intactivetop = 1;
                }
                else
                {
                    intactivetop = 0;
                }
                if (activeleft == "Có")
                {
                    intactiveleft = 1;
                }
                else
                {
                    intactiveleft = 0;
                }
                if (txtNameCategory.Text != "")
                {

                    if (fulImageCategory.HasFile)
                    {
                        if (LIB.Validate.CheckFileType(fulImageCategory.FileName))
                        {
                            LIB.Validate saveimage = new LIB.Validate();
                            filePathIamgeCate = "/Resources/Images/Image-Category/" + fulImageCategory.FileName + "";
                            saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Image-Category/"), fulImageCategory.FileName, 200, fulImageCategory.PostedFile.InputStream);
                            string values = "N'" + txtNameCategory.Text + "','" + filemap + "','" + CKEditorMota.Text + "','" + intactivetop + "','" + intactiveleft + "'";
                           

                            //pathToCreate = "~/Images/Image-Products/" + txtNameCategory.Text;
                            //if (Directory.Exists(Server.MapPath(pathToCreate)))
                            //{
                            //    Response.Write("<script>alert('Danh mục 1 này đã tồn tại...')</script>");
                            //}
                            //else
                            //{
                            //    Directory.CreateDirectory(Server.MapPath(pathToCreate));
                            //    DAL.DCollector.DBCollector.Instance.StoredInsert("tbl_Category",values);
                            //    Response.Write("<script>alert('Đã thêm mới thành công danh mục 1...')</script>");
                            //}
                        }
                        else
                        {
                            Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('Vui lòng upload ảnh danh mục...')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Vui lòng nhập tên danh mục...')</script>");
                }
            }
            if (lblIndex.Text.Equals("2"))
            {

                if (txtNameCategory.Text != "")
                {
                    if (fulImageCategory.HasFile)
                    {
                        if (LIB.Validate.CheckFileType(fulImageCategory.FileName))
                        {
                            cate1 = Cate1.Text;
                            LIB.Validate saveimage = new LIB.Validate();
                            filePathIamgeCate = "/Resources/Images/Image-Category/" + fulImageCategory.FileName + "";
                            saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Image-Category/"), fulImageCategory.FileName, 200, fulImageCategory.PostedFile.InputStream);
                            string values = "'" + cate1 + "',N'" + txtNameCategory.Text + "','" + filemap + "','" + CKEditorMota.Text + "',1,1";
                           
                            DAL.DCollector.DBCollector.Instance.StoredInsert("tbl_Category", values);
                            Response.Write("<script>alert('Đã thêm mới thành công danh mục 2...')</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('Vui lòng upload ảnh danh mục...')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Vui lòng nhạp tên danh mục...')</script>");
                }
            }
            if (lblIndex.Text.Equals("3"))
            {
                if (txtNameCategory.Text != "")
                {
                    if (fulImageCategory.HasFile)
                    {
                        if (LIB.Validate.CheckFileType(fulImageCategory.FileName))
                        {
                            cate2 = Cate2.Text;
                            LIB.Validate saveimage = new LIB.Validate();
                            filePathIamgeCate = "/Resources/Images/Image-Category/" + fulImageCategory.FileName + "";
                            saveimage.ThayDoiKichThuocAnh(Server.MapPath("/Resources/Images/Image-Category/"), fulImageCategory.FileName, 200, fulImageCategory.PostedFile.InputStream);
                            string values = "'" + cate2 + "',N'" + txtNameCategory.Text + "','" + filemap + "','" + CKEditorMota.Text + "',1,1";
                            DAL.DCollector.DBCollector.Instance.StoredInsert("tbl_Category", values);
                            Response.Write("<script>alert('Đã thêm mới thành công danh mục 3...')</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('Chỉ có thể upload file(.jpg,.png,.jpeg)')</script>");
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('Vui lòng upload ảnh danh mục...')</script>");
                    }
                }
                else
                {
                    Response.Write("<script>alert('Vui lòng nhập tên danh mục...')</script>");
                }
            }
        }
    }
}